//
//  singerTableViewCell.m
//  A段项目音乐播放
//
//  Created by 太阳 on 15/5/22.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "singerTableViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import "SingerModel.h"
#import <SDWebImage/SDImageCache.h>
#import <UIImageView+WebCache.h>
#import "singerActionView.h"
#import "SingerHeaderController.h"
#import "PendulumView.h"

@implementation singerTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self requestnet];
        self.selectionStyle = UITableViewCellAccessoryNone;
    }
    return self;
}
- (void)requestnet
{
    //歌手界面网络请求
    NSString *str =[NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=826886cd55c251bacd39b2d6523c12b126f3c590&idfa=27D84D88-B1C6-4173-B7C5-EE78CD26E7FB&utdid=VVrl99FV5JUDAA1STsAtAkrx&alf=201200&bundle_id=com.ttpod.music"];
    NSString *str1 = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    str = str1; //转换成UTF-8,不然可能出错误
    //    NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:url];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //告诉manager只下载原始数据,不要解析数据;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        //        NSString *html = [NSString stringWithString:responseObject];
        NSData *data = [NSData dataWithData:responseObject];
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@", dic);
        
        NSArray *array = [dic objectForKey:@"data"];
        self.modelArray = [NSMutableArray array];
        for (NSDictionary *modeldic in array) {
            SingerModel *model = [[SingerModel alloc]init];
            [model setValuesForKeysWithDictionary:modeldic];
            [self.modelArray addObject:model];
            [model release];
        }//我有了一个存满了model的数组
        
        [self addimage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)addimage
{
    int i = 0;
    CGFloat kongge = [UIScreen mainScreen].bounds.size.width * 0.02;
    CGFloat width = [UIScreen mainScreen].bounds.size.width / 3.2769;
    
    for (SingerModel *model in self.modelArray) {
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
        NSString *str = [model.pic_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [imageview sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil options:SDWebImageRetryFailed];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, width + kongge - 3, width, 20)];
        imageview.layer.cornerRadius = 5;
        label.text = model.title;
        label.textColor = [UIColor colorWithWhite:0.3 alpha:1];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15];
        singerActionView *view = [[singerActionView alloc]initWithFrame:CGRectMake(kongge+ i % 3 * (width + kongge), i / 3 * (width + kongge + 20), width, width + kongge * 2 + 20)];
        view.ID = [model.myId integerValue];
        [view clickedWithBlock:^(NSInteger temp) {
//            SingerHeaderController *header = [[SingerHeaderController alloc]init];
            [self.delegate sendId:temp
             ];
            
        }];
        [view addSubview:label];
        [view addSubview:imageview];
        [self.contentView addSubview:view];
        i++;
    }
}
@end
